angular.module('BaseServer')

.factory('BaseService', ['$resource', '$http', 'BaseFunction',
    function($resource, $http, BaseFunction) {
        return {
            //从别的服务器下载文件
            downloadFile: function(serverPath, downloadFileName, newFileName, deleteFileName, socketId, successHandle, errHandle){
                var parameter = {
                    ServerPath: serverPath,
                    DownloadFileName: downloadFileName,

                    FileName: newFileName,                      
                    DeleteFileName: deleteFileName,
                    SocketId: socketId
                };

                BaseFunction.request('/api/base/downloadFile', false, parameter, successHandle, errHandle);
            },
            //客户端下载本服务器文件
            clientDownloadFile: function(fileName, successHandle, errHandle){
                if(successHandle == undefined){
                    successHandle = BaseFunction.successFun;
                }

                if(errHandle == undefined){
                    errHandle = BaseFunction.errFun;
                }

                var parameter = {
                    FileName: fileName
                };
                
                $http.post('/api/base/getFileStream', parameter, {'responseType': 'arraybuffer'}).success(function (data, status, headers, config) {
                    var blob = new Blob([data], {type: "application/octet-binary"});
                    var aLink = document.createElement('a');
                    aLink.download = fileName;
                    aLink.href = window.URL.createObjectURL(blob);
                    aLink.click();
                    successHandle(fileName);
                }).error(function (data, status, headers, config) {
                    var newErr = {
                        data: {
                            leval: 5,
                            title: '请求失败',
                            message: data
                        }
                    };

                    errHandle(newErr);
                });
            },
            fileRenames: function(filePaths, successHandle, errHandle){
                var parameter = {
                    FilePaths: filePaths
                };

                BaseFunction.request('/api/base/fileRenames', false, parameter, successHandle, errHandle);
            },
            deleteFiles: function(filePaths, successHandle, errHandle){
                var parameter = {
                    FilePaths: filePaths
                };

                BaseFunction.request('api/base/fileDeletes', false, parameter, successHandle, errHandle);
            },
            createExcel: function(workbook, successHandle, errHandle){
                var parameter = {
                    Workbook: workbook
                };
                BaseFunction.request('/api/base/createExcel', false, parameter, successHandle, errHandle);
            },
            getSysConstValue: function(constName, successHandle, errHandle){
                var parameter = {
                    ConstName: constName
                };
                BaseFunction.request('/api/base/getSysConstValue', false, parameter, successHandle, errHandle);
            }
        };
    }
]);